#pragma once

#include "stack.h"
typedef struct {
    Stack push;
    Stack pop;
} MyQueue;


MyQueue* myQueueCreate() {
    MyQueue* Queue = (MyQueue*)malloc(sizeof(MyQueue));

    STInit(&Queue->push);
    STInit(&Queue->pop);
    return Queue;
}

void myQueuePush(MyQueue* obj, int x) {
    STPush(&obj->push, x);
}

int myQueuePop(MyQueue* obj) {
    if (STEmpty(&obj->pop))
    {
        while (STEmpty(&obj->push))
        {
            STPush(&obj->pop, STTop(&obj->push));
            STPop(&obj->push);
        }
    }

    assert(!STEmpty(&obj->pop));
    int ret = STTop(&obj->pop);
    STPop(&obj->pop);
    return ret;
}

int myQueuePeek(MyQueue* obj) {
    if (STEmpty(&obj->pop))
    {
        while (!STEmpty(&obj->push))
        {
            STPush(&obj->pop, STTop(&obj->push));
            STPop(&obj->push);
        }
    }

    assert(!STEmpty(&obj->pop));
    int ret = STTop(&obj->pop);
    return ret;
}

bool myQueueEmpty(MyQueue* obj) {
    return (STEmpty(&obj->pop) && STEmpty(&obj->push));
}

void myQueueFree(MyQueue* obj) {
    STDestroy(&obj->push);
    STDestroy(&obj->pop);
    free(obj);
}